home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.1 / Libraries / Intuition / other_examples / PubSc / commgad.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  2.6 KB  |  134 lines

  1. /* commgad.c -- create command gadgets */
  2.  
  3. /*
  4. Copyright (c) 1989 Commodore-Amiga, Inc.
  5.  
  6. Executables based on this information may be used in software
  7. for Commodore Amiga computers. All other rights reserved.
  8. This information is provided "as is"; no warranties are made.
  9. All use is at your own risk, and no liability or responsibility
  10. is assumed.
  11. */
  12.  
  13. #include <sysall.h>
  14.  
  15. #define D(x)    ;
  16.  
  17. static struct Image *cImage = NULL;    /* shared Image */
  18. static    UWORD        *cMask;
  19.  
  20. struct Image    *CreateImage();
  21. struct Image    *makeCommandImage();
  22. struct Gadget    *CreateBoolGadget();
  23. struct RastPort    *CreateImageRPort();
  24.  
  25. /*
  26.  *    creates linked list of "command" gadgets, with sequential
  27.  *    GadgetID values.
  28.  */
  29. struct Gadget    *
  30. commandGadgets( rp, argv, numgad, id )
  31. struct RastPort *rp;
  32. char    **argv;
  33. {
  34.     int                    num;
  35.     char                **a;
  36.     struct Rectangle    textent;
  37.     struct Gadget        *glist = NULL;
  38.     struct Gadget        *g;
  39.  
  40.     g = (struct Gadget *) &glist;
  41.  
  42.     textListExtent( rp, argv, numgad, &textent );
  43.  
  44.     if ( makeCommandImage( &textent ) )
  45.     {
  46.  
  47.         /* make gadget for each line of text */
  48.         for ( a = argv, num = numgad; num; ++a, --num )
  49.         {
  50.  
  51.             if ( g->NextGadget = CreateBoolGadget( cImage, cMask, id++, *a ) )
  52.             {
  53.                 g = g->NextGadget;
  54.                 D( printf("cG: gadget: %lx\n", g ) );
  55.  
  56.                 /* position gadget text */
  57.                 g->GadgetText->LeftEdge = (g->Width - textLength(rp, *a))/2;
  58.                 g->GadgetText->TopEdge = (g->Height - rp->TxHeight)/2;
  59.                     /*rp->TxBaseline + (g->Height - rp->TxHeight)/2;*/
  60.  
  61.                 /* set gadget flags */
  62.                 g->Activation |= RELVERIFY;
  63.             }
  64.             else
  65.             {
  66.                 FreeGadgets( glist );
  67.                 glist = NULL;
  68.                 freeCommandImage();
  69.                 break;
  70.             }
  71.         }
  72.     }
  73.     D( printf("cG return: %lx\n", glist ) );
  74.     return ( glist );
  75. }
  76.  
  77. cleanupCommGadgets()
  78. {
  79.     freeCommandImage();
  80. }
  81.  
  82. /*
  83.  * returns pointer to static command image, or NULL
  84.  */
  85. struct Image    *
  86. makeCommandImage( extent )
  87. struct Rectangle    *extent;
  88. {
  89.     struct RastPort    *rp;
  90.     int    width, height;
  91.     UWORD            *ImagePlane();
  92.  
  93.     width = extent->MaxX - extent->MinX + 1;
  94.     height = extent->MaxY - extent->MinY + 1;
  95.  
  96.     /* add some margin    */
  97.     width    += 16;            /* scale by resolution soon    */
  98.     height    += 6;
  99.  
  100.     if ( !cImage )
  101.     {
  102.         if ( cImage = CreateImage( width, height, 3 ) )
  103.         {
  104.  
  105.  
  106.             if ( rp = CreateImageRPort( cImage ) )
  107.             {
  108.                 drawBox( rp, width, height, 6, 3, 2, 1 );
  109.                 DeleteImageRPort( rp );
  110.  
  111.                 /* use third plane for mask    */
  112.                 cMask = ImagePlane( cImage, 2 );
  113.                 cImage->Depth = 2;    /* Do this AFTER ImageRPort! */
  114.             }
  115.             else
  116.             {
  117.                 freeCommandImage();        /* nulls out cImage */
  118.             }
  119.         }
  120.     }
  121.  
  122.     D( printf("mCI returning %lx\n", cImage ) );
  123.     return ( cImage );
  124. }
  125.  
  126. freeCommandImage()
  127. {
  128.     if ( cImage ) DeleteImage( cImage );
  129.     cImage = NULL;
  130. }
  131.  
  132.  
  133.  
  134.